package com.vivo.ic.dm;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Process;

public class DownloadWorker {
    private static HandlerThread sWorkerThread = new HandlerThread("worker_thread");
    static {
        sWorkerThread.start();
    }
    private static Handler sWorker = new Handler(sWorkerThread.getLooper());

    public static void runOnWorkerThread(Runnable r) {
        runOnWorkerThread(r, 0, Thread.NORM_PRIORITY);
    }

    public static void runOnWorkerThread(Runnable r, int priority) {
        runOnWorkerThread(r, 0, priority);
    }

    public static void runOnWorkerThread(Runnable r, long delayMillis, int priority) {
        sWorkerThread.setPriority(priority);
        if (sWorkerThread.getThreadId() == Process.myTid()) {
            r.run();
        } else {
            sWorker.postDelayed(r, delayMillis);
        }
    }

    public static void removeCallbacks(Runnable r) {
        sWorker.removeCallbacks(r);
    }
}
